#python3
import urllib.request
import urllib.parse
import json
import time
import base64
#from docx import Document
with open('timg.jpeg', 'rb') as f:  # 以二进制读取本地图片
    data = f.read()
    encodestr = str(base64.b64encode(data),'utf-8')
#请求头
headers = {
         'cookie': 'aliyun_choice=CN; UM_distinctid=16e83a8dc1cbf9-04d536dfb076c2-1d3b6a5d-13c680-16e83a8dc1db17; _ga=GA1.2.1060488531.1574166912; cna=kpX8FY0YmRICAXPNAMtGQhr6; login_aliyunid_pk=1568712732081117; aliyun_lang=zh; ping_test=true; t=930a754f8c69477633d5f5a1cdf0825b; _tb_token_=ee7da761157e5; cookie2=18e853f7a534863c250abd41e47c801c; _hvn_login=6; csg=8719dc80; login_aliyunid="zhangyanqian****@163.com"; login_aliyunid_ticket=YnbN5hossqIZCr6t7SGxRigm2Cb4fGaCdBZWIzmgdHq6sXXZQg4KFWufyvpeV*0*Cm58slMT1tJw3_j$$CjlcMDh_tm9NR3_ZF47fTDv25lBvHgJJr2tAOZmMn3of_BNpwU_TOTNChZBoeM1KJexdfz90; login_aliyunid_luid=BG+u09i77XMa0781b282c584efe54d1c2b0381fe8e7+fZaksc2Z8OzpqsQ6i5rSyUXmdC6X+ssUeXRwkhyX; login_aliyunid_csrf=_csrf_tk_1257179070513431; login_aliyunid_abi="BG+M5ma3LSj8c684724efb97a1796195afe6fe2cff3+C0JbyFPJSfPPqkjgGJ2c/6IJY5E3lQGavD6uR/ap8umf7Rm4Yxw="; login_aliyunid_pks="BG+/e+qHuPHXVAbyNHMT4TlhbuoerxglZrVYEKryueRD/w="; hssid=1wpmqqSymVAq_eO2o_xK8-w1; hsite=6; aliyun_country=CN; aliyun_site=CN; UC-XSRF-TOKEN=2f8fefb8-e046-475b-8de7-ac420570a655; FECS-XSRF-TOKEN=cf2782e5-4b91-43d1-99ba-7fe5775d422f; console_base_assets_version=3.5.4; FECS-UMID=%7B%22token%22%3A%22Y66c22ebd59e4d915ca8e4be5b782248b%22%2C%22timestamp%22%3A%226591525257595041544B607F%22%7D; _m_h5_c=fc11c3c747e838e4470aeebb89637090_1579231291159%3B18e2955e517ce916d554004fff39ac12; CNZZDATA1274719077=331806956-1579224877-%7C1579224877; l=cBEupsNmq0Wl9ubABOfwlurza77OrpRfksPzaNbMiIB19kCSGdFc0HwU-tkHh3QQE9CKPFKP5OMbMRLHRn6mG1JCsr4jfs5..; isg=BMHBNIbHezHQ9JT3cpCydi4-0Avb7jXgm0In2yMYfUlICsvcbjpGsQ9M7H5MAs0Y',
         'Content-Type': 'application/x-www-form-urlencoded',
         'referer':'https://duguang.aliyun.com/experience',
         'origin':'https://duguang.aliyun.com',
         'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
         'sec-fetch-mode': 'cors',
         'sec-fetch-site':'same-origin',
         'content-length': '529665',
         'accept-language': 'zh-CN,zh;q=0.9'

    }
def posturl(url,data={}):
  try:
    params=json.dumps(dict).encode(encoding='UTF8')
    req = urllib.request.Request(url, params, headers)
    r = urllib.request.urlopen(req)
    html =r.read()
    r.close();
    return html.decode("utf8")
  except urllib.error.HTTPError as e:
      print(e.code)
      print(e.read().decode("utf8"))
  time.sleep(1)
# def makedoc(data):
#     try:
#
#
#
#     except Exception as e:
#         print("makedoc exception"+e)
if __name__=="__main__":
    url_request="http://duguang.aliyun.com/ocrdemo/ocrDemoSecondService.json"
    dict = {'img': encodestr,
            'charInfo': 'true',
            'type': 'demo_general',
            'sign': 'e3d674dbd08ce417f1aeb89808cef7cb',
            'prob': 'true'
            }

    html = posturl(url_request, data=dict)
    print(html)